package com.xrj4j.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xrj4j.common.ResponseResult;
import com.xrj4j.vo.SponsorVO;
import com.xrj4j.entity.Sponsor;
import com.xrj4j.exception.BusinessException;
import com.xrj4j.mapper.SponsorMapper;
import com.xrj4j.service.SponsorService;
import com.walter.epay.param.request.EpayCreateOrderParam;
import com.walter.epay.param.response.EpayCreateOrderResponse;
import com.walter.epay.param.response.EpayResponse;
import com.walter.epay.paramenum.createorder.EpayCreateOrderIsHtmlEnum;
import com.walter.epay.requestapi.EpayRequestApi;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.security.NoSuchAlgorithmException;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class SponsorServiceImpl extends ServiceImpl<SponsorMapper, Sponsor> implements SponsorService {

    /**
     * 支付秘钥
     */
    private static final String KEY = "c9341871b4c146658417a4a83c07c4b4";

    /**
     * 商户id
     */
    private static final String MCH_ID = "1686022998";

    private final SponsorMapper sponsorMapper;

    @Override
    public ResponseResult createOrder(BigDecimal price, int payType) throws NoSuchAlgorithmException {
        EpayCreateOrderParam epayCreateOrderParam = new EpayCreateOrderParam();
        epayCreateOrderParam.setParam("test");
        epayCreateOrderParam.setIsHtml(EpayCreateOrderIsHtmlEnum.IS_JSON.getCode());
        epayCreateOrderParam.setPrice(price.doubleValue());
        epayCreateOrderParam.setType(payType);
        epayCreateOrderParam.setGoodsName("博客打赏");
        epayCreateOrderParam.setMchId(MCH_ID);
        epayCreateOrderParam.setPayId(String.valueOf(System.currentTimeMillis()));


        EpayResponse<EpayCreateOrderResponse> orderWithJson = EpayRequestApi.createOrderWithJson(epayCreateOrderParam, KEY);
        if (!orderWithJson.isSuccess()) {
            throw new BusinessException("订单创建失败，失败原因：" + orderWithJson.getMsg());
        }
        JSONObject jsonObject = JSON.parseObject(JSONUtil.toJsonStr(orderWithJson.getData()));
        Sponsor sponsor = Sponsor.builder().userId(StpUtil.getLoginIdAsString()).price(price)
                .orderId(jsonObject.get("orderId").toString()).isPay(0).payType(payType).build();
        baseMapper.insert(sponsor);

        log.info("订单创建成功，订单响应数据是：" + JSONUtil.toJsonStr(orderWithJson.getData()));
        return ResponseResult.success(orderWithJson.getData());
    }

    @Override
    public String callBack(String orderId, Double price, Double reallyPrice) {
        log.info("用户支付成功回调触发，orderId:{},price:{},reallyPrice:{}", orderId, price, reallyPrice);
        Sponsor sponsor = baseMapper.selectOne(new LambdaQueryWrapper<Sponsor>().eq(Sponsor::getOrderId, orderId));
        if (sponsor != null) {
            sponsor.setIsPay(1);
            baseMapper.updateById(sponsor);
            return "success";
        }
        return "error";
    }

    @Override
    public ResponseResult selectSponsorList() {
        List<SponsorVO> sponsorList = sponsorMapper.selectSponsorVOList();
        return ResponseResult.success(sponsorList);
    }
}
